import json
from pyecharts import options as opts
from pyecharts.charts import Bar



"""
现金流状况图：
-报告名称：REPORT_DATE_NAME
-经营净额     NETCASH_OPERATE
-筹资净额    NETCASH_FINANCE
-投资净额    NETCASH_INVEST
"""

def get_test_data():
    with open("cash_data.json","r") as fd:
        filedata = fd.read()
        json_data = json.loads(filedata)
        return json_data


def get_data(data, key):
    value = data.get(key)
    if value is None:
        value = 0
    return value


def make_xdata(data, fieldname):
    result = []
    count = data["result"]["count"]
    _data = data["result"]["data"]
    for _ in _data:
        result.append(get_data(_,fieldname))
    result.reverse()
    return result

def make_stack_data(keyname, data):
    result = []
    return result


def makechart_cash(data_cash):
    # 创建一个Bar对象
    bar = Bar()
    # 添加数据
    bar.add_xaxis(make_xdata(data_cash,"REPORT_DATE_NAME"))
    bar.add_yaxis("经营净额", make_xdata(data_cash,"NETCASH_OPERATE"))
    bar.add_yaxis("筹资净额", make_xdata(data_cash,"NETCASH_FINANCE"))
    bar.add_yaxis("投资净额", make_xdata(data_cash,"NETCASH_INVEST"))
    # 设置堆叠图
    bar.set_series_opts(stack="stack")
    # 设置全局选项
    bar.set_global_opts(title_opts=opts.TitleOpts(title="现金流状况图")) 
    # 渲染图表到文件
    bar.render("现金流量分析图.html")

if __name__ == '__main__':
    data = get_test_data()
    makechart_cash(data)
